def f(p1, p2="test", p3="test"):
    print "p1=", p1, "p2=", p2, "p3=", p3

def zigzag(n):
    for i in range(n):
        out = ""
        for j in range(n):
            c = i + j
            if c >= n:
                before = n * n
                sub = n - (c - (n - 1))
                ind = c - (n - 1)
                before -= sub * (sub + 1) / 2
                index = before + i - ind  if c % 2 == 1 else before + j - ind
            else:
                before = c * (c + 1) / 2
                index = before + i if c % 2 == 1 else before + j
            out += str(index) + " "
        print out
def zig(n,m):
    ret = []
    for i in range(n):
        ret.append([0 for w in range(m)])
        for j in range(m):
            c = i + j
            if(c < n):
                before = c * (c + 1) / 2
                ret[i][j] = before + i if c % 2 == 1 else before + j
    for i in range(n):
        for j in range(n):
            c=i+j
            if(c>=n):
                ret[i][j] = n * n - 1 - ret[n - 1 - i][n - 1 - j]
    for i in range(n):
        out=""
        for j in range(n):
            out+=str(ret[i][j])+" "
        print out

zig(5,5) 
